package org.example.compositebuffer;

import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.ByteBuffer;

/**
 * @author hh
 * @since 2024/11/2
 */
public class CompositeBufferToNIOBufferTest {
    public static void main(String[] args) {
        CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer(3);
        compositeByteBuf.addComponent(Unpooled.wrappedBuffer(new byte[]{1,2,3}));
        compositeByteBuf.addComponent(Unpooled.wrappedBuffer(new byte[]{4}));
        compositeByteBuf.addComponent(Unpooled.wrappedBuffer(new byte[]{5,6}));
        // 合并成一个Java NIO缓冲区
        ByteBuffer nioBuffer = compositeByteBuf.nioBuffer(0, 6);
        byte[] bytes = nioBuffer.array();
        System.out.println("bytes=");
        for (byte aByte : bytes) {
            System.out.println(aByte);
        }
        compositeByteBuf.release();
    }
}
